using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace MKDS_Course_Editor
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            //Application.VisualStyleState = System.Windows.Forms.VisualStyles.VisualStyleState.ClientAndNonClientAreasEnabled;
            Application.SetCompatibleTextRenderingDefault(false);
            Associations ass = new Associations();
            if (!Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software").GetSubKeyNames().Contains("MKDS Course Modifier"))
            {
                Microsoft.Win32.RegistryKey s = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software", true);
                Microsoft.Win32.RegistryKey k = s.CreateSubKey("MKDS Course Modifier");
                Microsoft.Win32.RegistryKey e = k.CreateSubKey("Extensions");
                foreach (string ss in Properties.Resources.Extensions.Split(','))
                {
                    e.SetValue(ss, 0);
                }
                k.SetValue("Version", Application.ProductVersion);
                //MessageBox.Show("Test");
                ass.ShowDialog();
            }
            Microsoft.Win32.RegistryKey ex = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("MKDS Course Modifier").OpenSubKey("Extensions", true);
            foreach (string ss in Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("MKDS Course Modifier").OpenSubKey("Extensions").GetValueNames())
            {
                if (!Properties.Resources.Extensions.Split(',').Contains(ss))
                {
                    ex.DeleteValue(ss);
                }
            }
            foreach (string ss in Properties.Resources.Extensions.Split(','))
            {
                if (!ex.GetValueNames().Contains(ss))
                {
                    ex.SetValue(ss, 0);
                }
            }
            if (!Application.ProductVersion.Equals(Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("MKDS Course Modifier").GetValue("Version")))
            {
                Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("MKDS Course Modifier", true).SetValue("Version", Application.ProductVersion);
                ass.ShowDialog();
            }
            if (args.Length > 0)
            {
                //Application.Run(new Form1(args[0]));
                openFile(args[0], null);
                return;
                //return;
            }
            else
            {
                Application.Run(new Form1());
            }
            if (System.IO.Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\MKDS temp\\"))
            {
                System.IO.Directory.Delete(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\MKDS temp\\", true);
            }
            if (System.IO.Directory.Exists("tmp"))
            {
                System.IO.Directory.Delete("tmp", true);
            }
        }
        public static void openFile(string name, Form parent)
        {
            openFile(name, System.IO.File.ReadAllBytes(name), parent);
        }
        public static void openFile(string name, byte[] data, Form parent)
        {
            switch (Convert.ToChar(data[0]).ToString() + Convert.ToChar(data[1]).ToString() + Convert.ToChar(data[2]).ToString() + Convert.ToChar(data[3]).ToString())
            {
                case "NARC":
                    break;
                case "NKMD":
                    nkm m = new nkm(name);
                    break;
                case "BMD0":
                    nsbmd d = new nsbmd(name);
                    d.ShowDialog();
                    break;
                case "BTX0":
                    NSBTX.nsbtx_ tx = new NSBTX.nsbtx_(name, false);
                    break;
                case "SDAT":
                    sdat s = new sdat(name);
                    break;
            }
        }
    }
}
